package com.wantup.demo01;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/6/25 14:28
 * <p>
 * <p>
 * 继承
 * 实现
 * 创建执行线程方式三。
 * 1 有返回值
 * 2 抛出异常
 * <p>
 * 需要FutureTask 实现类的支持 用于接受 运算结果 。
 * FutureTask是Future接口的实现类
 */
public class TestCallable {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

//        System.out.println(Thread.currentThread().getName());
//        new Test().start();
//        new Test().start();
//        new Thread(new Test02()).start();
//        new Thread(new Test02()).start();

        Test03 test03 = new Test03();
        FutureTask<Integer> futureTask = new FutureTask<>(test03);
        new Thread(futureTask).start();
        System.out.println(futureTask.get());

    }

}


class Test extends Thread {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

class Test02 implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

class Test03 implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        int num = 0;
        for (int i = 0; i < 10; i++) {
            num += i;
        }
        return num;
    }
}

